package com.ice.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

/**
 * Created by mrt on 2018/5/25.
 */
public class Oauth2Util {

    public static Map<String,Object> getJwtClaimsFromHeader(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        //取出头信息
        String authorization = request.getHeader("Authorization");
        if (StringUtils.isEmpty(authorization) || authorization.indexOf("Bearer") < 0) {
            return null;
        }
        //从Bearer 后边开始取出token
        String token = authorization.substring(7);
        Map<String,Object> map = null;
        try {
            //解析jwt
            Jwt decode = JwtHelper.decode(token);
            //得到 jwt中的用户信息
            String claims = decode.getClaims();
            //将jwt转为Map
            map = JSON.parseObject(claims, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    public static Map<String,Object> getJwtClaimsFromHeaderTest(String authorization) {

        if (StringUtils.isEmpty(authorization) || authorization.indexOf("Bearer") < 0) {
            return null;
        }
        //从Bearer 后边开始取出token
        String token = authorization.substring(7);
        Map<String,Object> map = null;
        try {
            //解析jwt
            Jwt decode = JwtHelper.decode(token);
            //得到 jwt中的用户信息
            String claims = decode.getClaims();
            //将jwt转为Map
            map = JSON.parseObject(claims, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static void main(String [] args){
        String authorization = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb21wYW55SWQiOiIxMjMiLCJ1c2VycGljIjpudWxsLCJ1c2VyX25hbWUiOiJpdGNhc3QiLCJzY29wZSI6WyJhcHAiXSwibmFtZSI6InRlc3QwMiIsInV0eXBlIjpudWxsLCJpZCI6NDksImV4cCI6MTU4NDI4MjEyMSwiYXV0aG9yaXRpZXMiOlsieGNfc3lzbWFuYWdlcl91c2VyIiwieGNfdGVhY2htYW5hZ2VyX2NvdXJzZV9saXN0IiwieGNfc3lzbWFuYWdlcl9yb2xlIiwieGNfdGVhY2htYW5hZ2VyX2NvdXJzZSIsImNvdXJzZV9maW5kX2xpc3QiLCJ4Y190ZWFjaG1hbmFnZXIiLCJ4Y19zeXNtYW5hZ2VyX21lbnUiXSwianRpIjoiODcxMDQzODEtN2NiZS00ZDBjLWJkMWItOGU0YThmNWJjYzBhIiwiY2xpZW50X2lkIjoiWGNXZWJBcHAifQ.BdauzL5GQnECdxiwopqzKg8Gi_weg8ozCiOgD-BlLKOUJUoe1D5tfmE-WuBUY-tAjljmPwu42YbliDBjsNOz8zyHBAowuU4zT1B65TqdP--DV_-9a5yDf6eeP_QEQyA44iEB0CO34nQtxLsm2nrbUeQuhujGlu_gdXLgKkcIm37zYnN_k7gdYS4SM0uIiivwSndBWAWXDrq7ty38bWArH-8c7CuOv0BWvsFQdu1W9e2E5gwaksBOZZasLZCBiGV4TV1uw39NG7o-wE0T5MleZGMmBubP3TnXF2OsY1Nj2i-NI9zZxwL9NN7n3oqC3h0raCjDUWtPFV7CNNWUr7KBQA";
        Map<String,Object>  map = getJwtClaimsFromHeaderTest(authorization);
        Set<String> keySet = map.keySet();
        for(String key : map.keySet()){
            System.out.print("Key = "+key);
        }
    }
}
